package Model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import DBAccess.DBAccess;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import MailGenerator.Mailer;

public class AccountManager {

	public Boolean loginFlag;

	public User myUser;

	public void login(String username, String password) {
	}

	public void logout() {
	}

	// Sends an Email to recover your password
	public static void sendPassword(String to, String password) {
		Mailer.sendMail(to, "Your UBC Dating Password!", 
					"Hello, You Have requested the password to your UBCDating" +
					" account. Your password is " + password);
	}

	// Email Validator that returns true if String is an email and false if
	public static boolean validateEmail(String email) {
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();

		if(matchFound){
		System.out.println("EMAIL OK");
		return true;
		}else{
		System.out.println("EMAIL ERROR");
		return false;
		}
	}

	//Send password to email
	public static void passwordRecovery(String email) {
		new DBAccess();
		Statement st = null;
		ResultSet rs = null;
		if (email != null && (validateEmail(email) == false)) {
			try {
				st = DBAccess.con.createStatement();
				rs = st.executeQuery("SELECT pword FROM accountdb where email='"
						+ email + "'");
				rs.next();
				String pword = rs.getString(1);
				sendPassword(email, pword);
			}

			catch (Exception e) {
				e.printStackTrace();
			}
		}
		DBAccess.close(st, rs);
	}

	// After checking whether the fields are valid, adds it to the database
	public static int addUser(String name, String email, String password1,
			String password2) {
		new DBAccess();
		PreparedStatement st = null;
		ResultSet rs = null;

		if (name == null || email == null || password1 == null
				|| password2 == null || (validateEmail(email) == false)) {
			return -1;
		}
		try {
			st = DBAccess.con.prepareStatement("SELECT email from accountdb WHERE email=?");
			st.setString(1, email);
			rs = st.executeQuery();
			
			if (rs.next() == false) {

				if (password1.equals(password2)) {
					int userID =  DBAccess.insertUser(password1, email, name);
					System.out.println(userID);
					if (userID >= 0){
						DBAccess.close(st, rs);
						DBAccess.insertAccount(userID);
						return userID;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		DBAccess.close(st, rs);
		return -1;
	}

	//returns the userid from email, a helper method for other functions
	public static int getUserID(String email) {
		new DBAccess();
		Statement st = null;
		ResultSet rs = null;
		if (email == null || (validateEmail(email) == false)) {
			return -1;
		}
		try {
			st = DBAccess.con.createStatement();
			rs = st.executeQuery("SELECT userID FROM accountdb where email='"
					+ email + "'");
			rs.next(); // check if rs.next() is null
			int userid = rs.getInt(1);
			return userid;
		}

		catch (Exception e) {
			e.printStackTrace();
		}
		DBAccess.close(st, rs);
		return -1;
	}

}